home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / Misc / getopt.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  3KB  |  98 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  * PDC I/O Library (C) 1987 by J.A. Lydiatt.
  5.  *
  6.  * This code is freely redistributable upon the conditions that this 
  7.  * notice remains intact and that modified versions of this file not
  8.  * be included as part of the PDC Software Distribution without the
  9.  * express consent of the copyright holders.  No warrantee of any
  10.  * kind is provided with this code.  For further information, contact:
  11.  *
  12.  *  PDC Software Distribution    Internet:                     BIX:
  13.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  14.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  15.  */
  16.  
  17. /*
  18.  * GetOpt.c Parse command line options
  19.  * 
  20.  * Modification history (LDH): optsign variable added, along with the ability to
  21.  * distinguish between '+arg' and '-arg'
  22.  */
  23.  
  24. #include    <stdio.h>
  25.  
  26. #define ERR(spat, cpat)   \
  27.     if (opterr) {   errbuf[0] = cpat; errbuf[1] = '\n';     \
  28.     (void) fwrite( argv[0], 1, strlen(argv[0]), stderr);    \
  29.     (void) fwrite( spat, 1, strlen(spat), stderr);              \
  30.     (void) fwrite( errbuf, 1, 2, stderr); }
  31.  
  32. extern int      strcmp();
  33. extern char    *index();
  34. extern int      strlen();
  35.  
  36. int   opterr = 1;
  37. int   optind = 1;
  38. int   optopt;
  39. char  optsign;
  40. char  optqual;
  41. char  *optarg;
  42.  
  43. int
  44. getopt(argc, argv, opts)
  45.     int             argc;
  46.     char          **argv, *opts;
  47. {
  48.     static int      sp = 1;
  49.     register int    c;
  50.     register char  *cp;
  51.     char            errbuf[2];
  52.  
  53.     if (sp == 1) {
  54.         if (optind >= argc)
  55.             return( EOF );
  56.         optsign = *argv[optind];
  57.         if ( (optsign != '-' && optsign != '+') || argv[optind][1] == '\0')
  58.             return (EOF);
  59.         else if (strcmp(argv[optind], "--") == NULL) {
  60.             optind++;
  61.             return (EOF);
  62.         }
  63.     }
  64.     optopt = c = argv[optind][sp];
  65.     if (c == ':' || (cp = index(opts, c)) == NULL) {
  66.         ERR(": illegal option -- ", c);
  67.         if (argv[optind][++sp] == '\0') {
  68.             optind++;
  69.             sp = 1;
  70.         }
  71.         return ('?');
  72.     }
  73.  
  74.     if (*++cp == ';')
  75.         optqual = argv[optind][++sp];
  76.  
  77.     if (*cp == ':' || *cp == ';') {
  78.         if (argv[optind][sp + 1] != '\0')
  79.             optarg = &argv[optind++][sp + 1];
  80.         else if (++optind >= argc) {
  81.             ERR(": option requires an argument -- ", c);
  82.             sp = 1;
  83.             return ('?');
  84.         }
  85.         else
  86.             optarg = argv[optind++];
  87.         sp = 1;
  88.     }
  89.     else {
  90.         if (argv[optind][++sp] == '\0') {
  91.             sp = 1;
  92.             optind++;
  93.         }
  94.         optarg = NULL;
  95.     }
  96.     return (c);
  97. }
  98.